#include		<stdlib.h>

#include		"list.h"

s_list			*list_add(s_list **list, void *value)
{
  s_list		*elem;

  if (NULL == (elem = malloc(sizeof(*elem))))
    return (NULL);
  elem->value = value;
  elem->next = *list;
  *list = elem;
  return (elem);
}

void			destruct_list(s_list *list)
{
  s_list *tmp;

  while (list)
  {
    tmp = list;
    list = list->next;
    free(tmp);
  }
}

void			list_foreach(s_list *list, void *arg,
    void (*cb)(void *, void *))
{
  while (list)
  {
    cb(list->value, arg);
    list = list->next;
  }
}

void			*list_find(s_list *list, void *elem,
    int (*filter)(void *, void *))
{
  while (list)
  {
    if (filter(list->value, elem))
      return (list->value);
    list = list->next;
  }
  return (NULL);
}
